package com.freesun.shop.service.impl;

import com.freesun.shop.model.WechatMessage;
import com.freesun.shop.service.WechatPushService;
import com.freesun.shop.task.WechatTokeRefreshTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class WechatPushServiceImpl implements WechatPushService {

    private static Logger logger = LoggerFactory.getLogger(WechatPushServiceImpl.class);

    private static final String WECHAT_PUSH_MSG_URL="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s";
    @Autowired
    private RestTemplate restTemplate;
    @Override
    public void pushMsg(WechatMessage msg) {
        logger.info("开始推送消息,本次推送的用户为:{}",msg.getToUser());
        //String token = "";
        String url = String.format(WECHAT_PUSH_MSG_URL, WechatTokeRefreshTask.token);
        String result = restTemplate.postForObject(url, msg, String.class);
        logger.info("本次发送的结果为:{}",result);
    }
}
